import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);

const store = new Vuex.Store({

  state: {
    // 存储token
    Authorization: localStorage.getItem('Authorization') ? localStorage.getItem('Authorization') : '',
    // 存储token时效
    AuthorizationTime: localStorage.getItem('AuthorizationTime') ? localStorage.getItem('AuthorizationTime') : '',
    // 存储username
    AuthorizationUsername: localStorage.getItem('AuthorizationUsername') ? localStorage.getItem('AuthorizationUsername') : '',
    // 存储password
    AuthorizationPassword: localStorage.getItem('AuthorizationPassword') ? localStorage.getItem('AuthorizationPassword') : '',
     // 存储user对象
    userData: localStorage.getItem('userData') ? JSON.parse(localStorage.getItem('userData')) : '',
    // 存储记住密码
    remberpwdchecked: localStorage.getItem('remberpwdchecked') ? localStorage.getItem('remberpwdchecked') : '',
    hasLogin: localStorage.getItem('hasLogin') ? localStorage.getItem('hasLogin') : '',
    muneid: localStorage.getItem('muneid') ? localStorage.getItem('muneid') : '',
  },

  mutations: {
    // 修改token，并将token存入localStorage
    changeLogin(state, user) {  //这里的state对应上面状态state
      //登录状态
      state.Authorization = user.Authorization;
      localStorage.setItem('Authorization', user.Authorization);
      //修改AuthorizationTime
      state.AuthorizationTime = user.AuthorizationTime;
      localStorage.setItem('AuthorizationTime', user.AuthorizationTime);
      //AuthorizationUsername
      state.AuthorizationUsername = user.AuthorizationUsername;
      localStorage.setItem('AuthorizationUsername', user.AuthorizationUsername);
      //修改AuthorizationPassword
      state.AuthorizationPassword = user.AuthorizationPassword;
      localStorage.setItem('AuthorizationPassword', user.AuthorizationPassword);
      //user对象
      state.userData = user.userData;
      localStorage.setItem('userData', JSON.stringify(user.userData))
    },
    //判断token是否存在，存在就进入首页，不存在就进入登录页
    logine(state) {
      localStorage.setItem('hasLogin', true);
    },
    logout() {
      localStorage.setItem('hasLogin', false);
    },
    changePassword(state, user) {
      //修改AuthorizationPassword
      state.AuthorizationPassword = user.AuthorizationPassword;
      localStorage.setItem('AuthorizationPassword', user.AuthorizationPassword);
    },
    changeremberpwdchecked(state, user) {
      //修改AuthorizationPassword
      state.remberpwdchecked = user.remberpwdchecked;
      localStorage.setItem('remberpwdchecked', user.remberpwdchecked);
    },
    changemenuid(state, user) {
      //修改AuthorizationPassword
      state.muneid = user.muneid;
      localStorage.setItem('muneid', user.muneid);
    }
  }
});


export default store